#include "utils/git_detector.h"
#include <iostream>
#include <fstream>

#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

std::vector<std::string> GitDetector::getDefaultSearchPaths() {
    return {
        "C:/Program Files/Git/bin/git.exe",
        "C:/Program Files (x86)/Git/bin/git.exe",
        "/mingw64/bin/git.exe",
        "/usr/bin/git",
        "/bin/git",
        "git"  // 最后尝试 PATH 中的
    };
}

bool GitDetector::checkGitExecutable(const std::string& path) {
    std::string command = path + " --version > /dev/null 2>&1";
    #ifdef _WIN32
    command = "\"" + path + "\" --version > nul 2>&1";
    #endif
    
    int result = system(command.c_str());
    return result == 0;
}

std::string GitDetector::findGitPath() {
    auto possible_paths = getDefaultSearchPaths();
    
    for (const auto& path : possible_paths) {
        if (checkGitExecutable(path)) {
            std::cout << "✅ Found Git: " << path << std::endl;
            return path;
        }
    }
    
    std::cerr << "❌ Git not found. Please install Git or set PATH" << std::endl;
    return "";
}
